﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Data.SqlClient;
//using System.Configuration;

using ECS.DAL;
using ECS.DAL.Component;
using ECS.Framework;

namespace E_ICS.E_ICS
{
    public partial class frmEditSelectWeightId : Form
    {
        #region  private properties
        private DataSet dtSelectWeight = new DataSet();
        #endregion

        #region form events
        

        //SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["con3"].ConnectionString);
        public frmEditSelectWeightId()
        {
            InitializeComponent();
            
        }
        private void frmEditSelectWeightId_Load(object sender, EventArgs e)
        {
            try
            {
                showdgvDimensionDisplay();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void dgvWeightAllId_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        
        private void dgvWeightAllId_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex >= 0)
                {
                    dgvWeightAllId.Rows[e.RowIndex].Selected = true;
                    InsertToValueEditWeight(e.RowIndex);
                    E_ICS.frmEditWeightEntry ewe = new E_ICS.frmEditWeightEntry();
                    if (ewe.ShowDialog() == DialogResult.Yes)
                    {
                        showdgvDimensionDisplay();
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }
        #endregion

        #region private methods
        public void showdgvDimensionDisplay()
        {
            WeightRecordComponent compData = new WeightRecordComponent();
            dtSelectWeight = compData.GetSelectWeight(ViewEditWeightSlip.MAWB_ID);

            //StringBuilder sb = new StringBuilder();
            //sb.Append("select * from v_weight where mawb_id = '@mawb_id' ");
            //sb.Replace("@mawb_id", ViewEditWeightSlip.MAWB_ID);
            //using (Entities entities = new Entities())
            //{
            //    entities.Open();
            //    dtSelectWeight = entities.GetDataBySQL(sb.ToString(), "dtDimDetail");
            //}
            
            DataTable dtWeightAllId_TB = new DataTable();
            dtWeightAllId_TB = CreatedgvWeightAllId_TB(dtSelectWeight);
            dgvWeightAllId.DataSource = dtWeightAllId_TB;

            //SqlCommand cmd = new SqlCommand("select * from v_weight where mawb_id = @mawb_id ", sqlCon);
            //cmd.Parameters.AddWithValue("@mawb_id", ViewEditWeightSlip.MAWB_ID);
            ////cmd.Parameters.AddWithValue("@mawb_id", MAWB_id);
            //sqlCon.Open();

            //SqlDataAdapter daLocationDetail = new SqlDataAdapter(cmd);
            //dtSelectWeight = new DataSet();
            //daLocationDetail.Fill(dtSelectWeight, "dtDimDetail");
            //DataTable dtWeightAllId_TB = new DataTable();
            //dtWeightAllId_TB = CreatedgvWeightAllId_TB(dtSelectWeight);
            //dgvWeightAllId.DataSource = dtWeightAllId_TB;

            //sqlCon.Close();


        }
        private DataTable CreatedgvWeightAllId_TB(DataSet dtSWeight)
        {
            DataTable TLWeightAllId = new DataTable();
            ///////////create head column/////////////////
            TLWeightAllId.Columns.Add("pallet count", typeof(string));
            TLWeightAllId.Columns.Add("gross weight", typeof(string));
            TLWeightAllId.Columns.Add("tare weight", typeof(string));
            TLWeightAllId.Columns.Add("net weight", typeof(string));

            ///////////////////////////////////////////////
            int row = dtSWeight.Tables[0].Rows.Count;
            for (int i = 0; i < row; i++)
            {
                string pallet_count = dtSWeight.Tables[0].Rows[i]["pallet_count"].ToString().Trim();
                string gross_weight = dtSWeight.Tables[0].Rows[i]["gross_weight"].ToString().Trim();
                string tare_weight = dtSWeight.Tables[0].Rows[i]["tare_weight"].ToString().Trim();
                string net_weight = dtSWeight.Tables[0].Rows[i]["net_weight"].ToString().Trim();

                TLWeightAllId.Rows.Add(pallet_count, gross_weight, tare_weight, net_weight);
            }
            return TLWeightAllId;
        }

        private void InsertToValueEditWeight(int p)
        {

            ViewEditWeightId.editwawb_Id = dtSelectWeight.Tables[0].Rows[p]["mawb_id"].ToString().Trim();
            ViewEditWeightId.editweight_Id = dtSelectWeight.Tables[0].Rows[p]["weight_id"].ToString().Trim();
            ViewEditWeightId.editslip_Id = dtSelectWeight.Tables[0].Rows[p]["slip_id"].ToString().Trim();
            ViewEditWeightId.pallet_count = dtSelectWeight.Tables[0].Rows[p]["pallet_count"].ToString().Trim();
            ViewEditWeightId.gross_weight = dtSelectWeight.Tables[0].Rows[p]["gross_weight"].ToString().Trim();
            ViewEditWeightId.tare_weight = dtSelectWeight.Tables[0].Rows[p]["tare_weight"].ToString().Trim();
            ViewEditWeightId.net_weight = dtSelectWeight.Tables[0].Rows[p]["net_weight"].ToString().Trim();
            ViewEditWeightId.weight_number = dtSelectWeight.Tables[0].Rows[p]["weight_number"].ToString().Trim();

        }
        #endregion

        

    }
}
